<?php

use app\assets\ApiAsset;
use app\assets\LayerAsset;
use app\assets\TableAsset;
use app\widgets\ManagerTableOp;
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * @var $this yii\web\View
 * @var $agentPackageList app\models\AgentPackage[]
 */

ApiAsset::register($this);
LayerAsset::register($this);
TableAsset::register($this);

$this->title = '套餐列表';
$this->params['breadcrumbs'][] = '物联网卡管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<table class="table table-striped table-bordered table-hover">
    <thead>
        <tr>
            <th class="center">
                <label class="pos-rel">
                    <input type="checkbox" class="ace" />
                    <span class="lbl"></span>
                </label>
            </th>
            <th>名称</th>
            <th>流量</th>
            <th>成本价（月）</th>
            <th>销售价（月）</th>
            <th>操作</th>
        </tr>
    </thead>

    <tbody>
        <?php foreach ($agentPackageList as $agentPackage) {?>
            <tr>
                <td class="center"><label class="pos-rel"><input type="checkbox" class="ace" value="<?php echo $agentPackage->id;?>" /><span class="lbl"><?php echo $agentPackage->id;?></span></label></td>
                <td><?php echo Html::encode($agentPackage->package->name);?></td>
                <td><?php echo Yii::$app->formatter->asShortSize($agentPackage->package->data);?></td>
                <td><?php echo $agentPackage->cost_price;?></td>
                <td><?php echo Html::textINput('sale_price[' . $agentPackage->id . ']', $agentPackage->sale_price, ['class' => 'form-control', 'style' => 'max-width: 100px;']);?></td>
                <td><?php echo ManagerTableOp::widget(['items' => [
                        ['icon' => 'fa fa-info-circle', 'href' => Url::to(['/agent/card/package-view', 'id' => $agentPackage->id]), 'btn_class' => 'btn btn-xs', 'tip' => '详情'],
                        ['icon' => 'fa fa-save', 'onclick' => 'saveSalePrice(' . $agentPackage->id . ')', 'btn_class' => 'btn btn-xs btn-success', 'tip' => '保存销售价'],
                    ]]);?></td>
            </tr>
        <?php }?>
    </tbody>
</table>
<script>
    /**
     * 保存销售价
     * @param id 代理商套餐编号
     */
    function saveSalePrice(id) {
        var salePrice = $('[name="sale_price[' + id + ']"]').val();
        $.getJSON('<?php echo Url::to(['/agent/card/save-sale-price']);?>', {id: id, sale_price: salePrice}, function (json) {
            if (callback(json)) {
                layer.msg('已保存。');
            }
        });
    }
</script>
